﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuceneSearcher;
using NUnit.Framework;
using Snap.Model;

namespace Tests.Integration
{
    [TestFixture]
    public class WhenAnalyzingSearchResults
    {
        private const long NumTicksInSecond = 10000 * 1000;
        private SnapModelContainer context;

        [SetUp]
        public void SetUp()
        {
            context = new SnapModelContainer();
        }
       
        [Test]
        public void GetAllNearbyPricesWorks01()
        {
            //News with Id [5123] most closely matched NewsItem with Id [5029]
            var newsId = 5123;
            var publishTimeUTC = context.NewsItems
                .Where( x => x.Id == newsId )
                .Select( x => x.PublicationDate )
                .FirstOrDefault();
            var pubTicks = context.NewsItems
                .Where( x => x.Id == newsId )
                .Select( x => x.PublicationDate )
                .FirstOrDefault().AddHours( -5 ).Ticks;
            var marketDate = new DateTime( pubTicks ).Date;
            var symbol = "AAPL";
            var selectedTickers = context.Tickers
                .Where( x => x.CompanySymbol == symbol && x.MarketDate == marketDate
                             && x.Tick >= pubTicks &&
                             x.Tick < (pubTicks + (60 * 60 * NumTicksInSecond)) )
                .ToList();
            var priceAtEvent = selectedTickers.First().BuyPriceAvg;
            var priceAtOneMin = selectedTickers.Where( x => x.Tick >= (pubTicks + 1 * 60 * NumTicksInSecond) )
                .First().BuyPriceAvg;
            var priceAtTwoMin = selectedTickers.Where( x => x.Tick >= (pubTicks + 2 * 60 * NumTicksInSecond) )
                .First().BuyPriceAvg;

            var searchResult = new SearchResult {NewsId = 5123, BestMatchNewsId = 5029, BestMatchScore = 10.0d};
            var priceAnalyses = MarketDataAnalyzer.MarketDataAnalyzer.Analyze( new List<SearchResult> {searchResult} );

            Assert.That( priceAnalyses.Count(), Is.EqualTo( 1 ) );
            var priceAnalysis = priceAnalyses.First();
            Assert.That( priceAnalysis.Symbol, Is.EqualTo( symbol ) );
            Assert.That( priceAnalysis.MatchingNewsId, Is.EqualTo( searchResult.BestMatchNewsId ) );
            Assert.That(priceAnalysis.GetActualPriceAtRelativeTime(0), Is.EqualTo( priceAtEvent )  );

        }
    }
}
